﻿using Basic.FlowApplication.Interface;
using Basic.FlowRemoteModel;
using WeDonekRpc.Client.Interface;
using WeDonekRpc.Helper;

namespace Basic.FlowApplication.AuditLocalEvent.AuditEvent
{
    /// <summary>
    /// 作废签名文件
    /// </summary>
    internal class ToVoidSignFile : IEventHandler<FlowAuditEvent>
    {
        private readonly IAuditSignLogService _Service;

        public ToVoidSignFile ( IAuditSignLogService service )
        {
            this._Service = service;
        }

        public void HandleEvent ( FlowAuditEvent data, string eventName )
        {
            if ( data.Item.Status == FlowAuditItemStatus.已回退 || data.Item.Status == FlowAuditItemStatus.打回修改 )
            {
                FlowNodeAuditStatus[] status = new FlowNodeAuditStatus[]
                {
                    FlowNodeAuditStatus.审核通过,
                    FlowNodeAuditStatus.审核进行中
                };
                long[] nodeId = data.NodeState.Where(a => a.Value.OptItemId == data.ItemId && status.Contains(a.Value.PrevStatus)).Select(a => a.Key).ToArray();
                if ( !nodeId.IsNull() )
                {
                    long[] itemId = data.Log.Items.Where(a => ( nodeId.Contains(a.NodeId) && a.Status == FlowAuditItemStatus.审核通过 ) || a.OptItemId == data.ItemId).Select(a => a.Id).ToArray();
                    this._Service.ToVoid(itemId, data.ItemId);
                }
            }
        }
    }
}
